package com.yr.ryjk.expert;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.doctor.entity.FilterParam;
import com.yr.ryjk.expert.entity.DoctorExpert;
import com.yr.ryjk.expert.entity.ExpertType;
import com.yr.ryjk.expert.service.IExpertService;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.util.IDGenerator;
import com.yr.ryjk.util.JSONUtil;

/**
 * 名医推荐模块
 * @author Luning
 *
 */
@For(user=UserType.WEBSERVER_USER)
@Controller
@RequestMapping("/expert")
public class ExpertController {
	@Autowired
	private IExpertService expertService;
	/**
	 * 后台分页获取名医分类接口(list)
	 * @param pageNo
	 * @param pageSize
	 * @return 
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getList(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize){
		return JSONUtil.getJSON(ErrorType.SUCCESS,expertService.getAll((pageNo-1)*pageSize,pageSize));
	}
	
	/**
	 * 后台增加名医推荐分类接口(add)
	 * @param name
	 * @param description
	 * @param logoUrl
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addInfo(@RequestParam("name") @NotBlank String name,
			@RequestParam("description") @NotBlank String description,
			@RequestParam("logoUrl") @NotBlank String logoUrl) throws IllegalAccessException, InvocationTargetException{
		ExpertType expertType = new ExpertType();
		expertType.setId(IDGenerator.gen32UUID());
		expertType.setLogoUrl(logoUrl);
		expertType.setName(name);
		expertType.setDescription(description);
		return expertService.addInfo(expertType);
	}
	
	/**
	 * 后台修改名医分类信息接口(modify)
	 * @param id
	 * @param name
	 * @param description
     * @param logoUrl
	 * @return
	 */
	@RequestMapping(value="/modify",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateInfo(@RequestParam("id") @NotBlank String id,
			@RequestParam("name") @NotBlank String name,
			@RequestParam("logoUrl") @NotBlank String logoUrl,
			@RequestParam("description") @NotBlank String description){
		ExpertType expertType = new ExpertType();
		expertType.setId(id);
		expertType.setName(name);
		expertType.setLogoUrl(logoUrl);
		expertType.setDescription(description);
		expertService.updateInfo(expertType);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	/**
	 * 删除名医分类
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/del",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("ids") @NotBlank String ids){
		expertService.deleteByType(ids);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 后台或app获取指定医生所属的所有分类接口(listOfDoctor)
	 * @param docId
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/listOfDoctor",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listOfDoctor(@RequestParam("docId") @NotBlank String docId){
		return JSONUtil.getJSON(ErrorType.SUCCESS,expertService.getAllExpertTypeOfDoctor(docId));
	}
	
	/**
	 * 后台为名医分类添加医生接口(fill)
	 * @param typeId
	 * @param docId
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/fill",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addExpertTypeForDocId(@RequestParam("typeId") @NotBlank String typeId,@RequestParam("docId") @NotBlank String docId) throws IllegalAccessException, InvocationTargetException{
		DoctorExpert de = new DoctorExpert();
		de.setId(IDGenerator.gen32UUID());
		de.setDocId(docId);
		de.setExpertTypeId(typeId);
		return expertService.addDoctorExpertInfo(de);
	}
	
	/**
	 * 后台为从指定分类中删除所属的n个医生接口(pop)
	 * @param docId
	 * @return
	 */
	@RequestMapping(value="/pop",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteByDocId(@RequestParam("typeId") @NotBlank String typeId,
			@RequestParam("docIds") @NotBlank String docIds){
		return expertService.deleteByDocId(typeId,docIds);
	}
	
	/**
	 * app获取所有名医分类接口(listAll)
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/listAll",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAllExpertType(){
		return JSONUtil.getJSON(ErrorType.SUCCESS,expertService.getAllExpertType());
	}
	
	/**
	 * app和后台分页获取属于指定名医分类的医生接口(listOfType)
	 * @param pageNo
	 * @param pageSize
	 * @param typeId
	 * @return
	 */
	@For(user={UserType.APP_USER,UserType.WEBSERVER_USER})
	@RequestMapping(value="/listOfType",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listOfType(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			@RequestParam("typeId") @NotBlank String typeId,
			String token,FilterParam filterParam){
		return JSONUtil.getJSON(ErrorType.SUCCESS,
				expertService.getListOfType(pageNo, pageSize, typeId, token,filterParam));
	}
	
	/**
	 * 通过主键id获取分类表里的数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getInfo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getInfo(@RequestParam("id") @NotBlank String id){
		return JSONUtil.getJSON(ErrorType.SUCCESS,expertService.getInfoById(id));
	}
}